package com.naodong.shenluntiku.mvp.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.naodong.shenluntiku.R;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

/**
 * Created by hujie on 17/5/25.
 */

public class CodeInputView extends android.support.v7.widget.AppCompatEditText {

    private static final int defaultContMargin = 5;
    private static final int defaultSplitLineWidth = 1;

    private int borderColor = 0xFFCCCCCC;
    private float borderWidth = 1;
    private float borderRadius = 3;

    private int codeLength = 6;
    private int codeColor = 0xFF333333;
    private int codeSize;

    private float splitLineWidth = defaultSplitLineWidth;

    private Paint codePaint = new Paint(ANTI_ALIAS_FLAG);
    private Paint borderPaint = new Paint(ANTI_ALIAS_FLAG);


    int mWidth;
    int mHeight;
    RectF outRectf;
    RectF innerRect;
    private CharSequence text = "";

    InputCompleteListener inputCompleteListener;

    public CodeInputView(Context context, AttributeSet attrs) {
        super(context, attrs);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        borderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, borderWidth, dm);
        borderRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, borderRadius, dm);
        codeLength = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, codeLength, dm);
        codeSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, dm);
        splitLineWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, splitLineWidth, dm);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CodeInputView, 0, 0);
        borderColor = a.getColor(R.styleable.CodeInputView_pivBorderColor, borderColor);
        borderWidth = a.getDimension(R.styleable.CodeInputView_pivBorderWidth, borderWidth);
        borderRadius = a.getDimension(R.styleable.CodeInputView_pivBorderRadius, borderRadius);
        codeLength = a.getInt(R.styleable.CodeInputView_pivCodeLength, codeLength);
        codeColor = a.getColor(R.styleable.CodeInputView_pivCodeColor, codeColor);

        splitLineWidth = a.getDimension(R.styleable.CodeInputView_pivCodeWidth, splitLineWidth);

        a.recycle();

        codePaint.setStyle(Paint.Style.FILL);
        codePaint.setTextSize(codeSize);
        codePaint.setColor(codeColor);
        init();
    }

    private void init() {

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        if (changed) {
            mWidth = getWidth();
            mHeight = getHeight();
            outRectf = new RectF(0, 0, mWidth, mHeight);
            innerRect = new RectF(outRectf.left + defaultContMargin, outRectf.top + defaultContMargin,
                    outRectf.right - defaultContMargin, outRectf.bottom - defaultContMargin);
        }
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        // 外边框
        borderPaint.setStrokeWidth(borderWidth);
        borderPaint.setColor(borderColor);
        canvas.drawRoundRect(outRectf, borderRadius, borderRadius, borderPaint);


        // 内容区
        borderPaint.setColor(Color.WHITE);
        canvas.drawRoundRect(innerRect, borderRadius, borderRadius, borderPaint);

        // 分割线
        borderPaint.setColor(borderColor);
        borderPaint.setStrokeWidth(splitLineWidth);
        for (int i = 1; i < codeLength; i++) {
            float x = width * i / codeLength;
            canvas.drawLine(x, 0, x, height, borderPaint);
        }
        // 密码
        float cx, cy = height / 2;
        float half = width / codeLength / 2;
        for (int i = 0; i < text.length(); i++) {

            String drawText = text.subSequence(i, i + 1).toString();
            cx = width * i / codeLength + half;
            Paint.FontMetrics fontMetrics = codePaint.getFontMetrics();
            // 计算文字高度
            float fontHeight = fontMetrics.bottom - fontMetrics.top;
            float textWidth = codePaint.measureText(drawText);
            // 计算文字baseline
            float baseline = (innerRect.bottom + innerRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
            canvas.drawText(drawText, cx - textWidth / 2, baseline, codePaint);

        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if (text.length() > codeLength) {
            return;
        }
        this.text = text;
        invalidate();
        if (text.length() == codeLength) {
            if (inputCompleteListener != null) {
                inputCompleteListener.onInputComplete(text.toString());
            }
        }
    }


    public void clear() {
        setText("");
    }

    public interface InputCompleteListener {
        void onInputComplete(String code);
    }

    public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {
        this.inputCompleteListener = inputCompleteListener;
    }
}

